import { isUrl } from "../utils/utils";
import { getAuthority} from "../utils/authority";

const menuData = [
  {
    name: "家谱",
    icon: "dashboard",
    path: "dashboard",
    children: [
      {
        name: "家谱(编辑)",
        path: "familytree",
        authority: getAuthority()
      },
      {
        name: "家谱(只读)",
        path: "familytree_readonly"
      },
      {
        name: "谷峰居简介",
        path: "familyIntroduction"
      }
    ]
  },
  {
    name: "家族事务",
    icon: "table",
    path: "familyAffair",
    children: [
      {
        name: "成员通讯录",
        path: "familyContact"
      },
      {
        name: "家族公告",
        path: "familyNotice"
      },
      {
        name: "公共开支",
        path: "honorList"
      }
    ]
  }
];

function formatter(data, parentPath = "/", parentAuthority) {
  return data.map(item => {
    let { path } = item;
    if (!isUrl(path)) {
      path = parentPath + item.path;
    }
    const result = {
      ...item,
      path,
      authority: item.authority || parentAuthority
    };
    if (item.children) {
      result.children = formatter(
        item.children,
        `${parentPath}${item.path}/`,
        item.authority
      );
    }
    return result;
  });
}

export const getMenuData = () => formatter(menuData);
